<?php 
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
class GroupController extends AdminBaseController{
	public function index(){
		$title = I('get.title/s','');
		$page = I('get.page/d',1);
		$group = M('group');
		$whereSql = empty($title) ? '' :'title like "%'.$title.'%"';
		$num = 5;
		$totalnum = $group -> where('status = 1') -> count('id');
		$pagenum= ceil($totalnum / $num);
		$frow = empty($page - 1) ? 0 : (($page -1) * $num);
		$list = $group -> where($whereSql) -> order('id asc') -> limit($frow,$num) -> select();
		$this -> assign('glist',$list);
		$this -> assign('pagenum',$pagenum);
		$this -> assign('title',$title);
		$this -> display();
	}
	//分组添加
	public function add(){
		$this -> display();
	}
	//分组添加保存
	public function addsave(){
		if(IS_POST || IS_AJAX){
			$title = I('post.title/s','');
			if(!$title){
				$this -> ajaxReturn(['status' => 0, 'mess' => '组名不能为空！']);
			}
			$status = I('post.status/d');
			$group = M('group');
			$groupInfo = $group -> where(['title' => $title]) -> find();
			if($groupInfo){
				$this -> ajaxReturn(['status' => 0, 'mess' => '分组名已存在！']);
			}
			$data = ['title' => $title, 'status' => $status];
			$gid = $group -> add($data);
			if($gid === false){
				$this -> ajaxReturn(['status' => 0 , 'mess' => '分组添加失败！']);
			}
			$this -> ajaxReturn(['status' => 1, 'mess' => '分组添加成功！']);
		}
		 $this -> ajaxReturn(['status' => 0, 'mess' => '非法操作！']);
	}
	//查询一条用find 多条select
	public function edit(){
		if(IS_GET){
			$id = I('get.id/d',0);
			if(!$id){
				$this -> ajaxReturn(['status' => 0, 'mess' => '非法操作!']);
			}
			$getInfo = M('group') -> field('id,title,status') -> where(['id' => $id]) -> find();
			$this -> assign('getInfo',$getInfo);
			$this -> display();
		}
	}
	public function editsave(){
		if(IS_AJAX || IS_POST){
			$id = I('post.id/d',0);
			if(!$id){
				$this -> ajaxReturn(['status' => 0,'mess' => '非法操作！']);
			}
			$title = I('post.title/s','');
			if(!$title){
				$this -> ajaxReturn(['status' => 0,'mess' => '分组名不能为空！']);
			}
			$status = I('post.status/d',0);
			$group = M('group');
			$groupInfo = $group -> where(['id' => $id]) -> find();
			if(empty($groupInfo)){
				$this -> ajaxReturn(['status' => 0, 'mess' => '分组不存在无法修改！']);
			}
			$data = ['title' => $title,'status' => $status];
			$gid = $group -> where(['id' => $id]) -> save($data);
			if($gid === false){
				$this -> ajaxReturn(['status' => 0, 'mess' => '修改失败！']);
			}
			$this -> ajaxReturn(['status' => 1,'mess' => '修改成功！']);
		}
		$this -> ajaxReturn(['stauts' => 0,'mess' => '非法操作！']);
	}

	public function rule(){
		//通过分组列表页面来添加分配权限通过id
		if(IS_GET){
			$id = I('get.id/d',0);
			if(!$id){
				$this -> ajaxReturn(['status' => 0, 'mess' => '非法操作！']);
			}
			//通过分组表来查找添加字段的值展示出来
			$rule = M('Group') -> field('rules') -> where(['id' => $id]) -> find();
			//通过查找规则表把所有的权限以数组的形式查出来
			$rulelist = M('Rule') -> field('id,pid,title') -> where(['status' => 1]) -> select(); 
			$data = makeTree($rulelist);//将查出来的数组经过make_tree进行遍历
			unset($rulelist);//销毁查询出来的内容减少资源消耗
			$this -> assign('id',$id);
			$this -> assign('rule',explode(',',$rule['rules']));//也许一个人拥有多个权限所以pid比较多以，隔开变成数组
			$this -> assign('datalist',$data);//将遍历过的等级进行展示给前台
			$this -> display();
		}else{
			$id = I('post.id/d',0);
			$rule_ids = I('post.rule_ids/a',[]);
			$is_up = false;//默认给变量是false
			if($id && !empty($rule_ids)){
				$is_up = M('Group') -> where(['id' => $id]) -> setField(['rules'=>implode(',', $rule_ids)]);
				// echo $is_up;exit;针对setField 更新 什么都没改的话需要全等于false
			}
			if($is_up === false){
				$this -> ajaxReturn(['status' => 0, 'mess' => '更新失败！']);
			}else{
				$this -> ajaxReturn(['status' => 1, 'mess' => '更新成功！']);
			}
		}
	}

	public function del(){
		if(IS_POST){
			$id = I('post.id/d',0);
			if(empty($id) || (1 == $id)){
				$this -> ajaxReturn(['status' => 0, 'mess' => '无法操作']);
			}
			$isDel = M('group') -> where(['id' => $id]) -> delete();
			if(!$isDel){
				$this -> ajaxReturn(['status' => 0, 'mess' => '删除失败！']);
			}
			$this -> ajaxReturn(['status' => 1, 'mess' => '删除成功！']);
		}
		$this -> ajaxReturn(['status' => 0,'mess' => '非法操作！']);
	}
	public function Alterstate(){
		if(IS_POST){
			$id = I('post.id/d',0);
			if(empty($id) || (1 == $id)){
				$this -> ajaxReturn(['status' =>0,'mess' => '非法操作！']);
			}
			$groupInfo = M('group') -> field('status') ->where(['id' => $id]) -> find();
			if($groupInfo['status'] == 0){
				M('group') -> where(['id' => $id]) -> setField(['status' => 1]);
				$this -> ajaxReturn(['status' => 1, 'mess'=> '已开启','data'=> 1]);
			}else{
				M('group') -> where(['id' => $id]) -> setField(['status' => 0]);
				$this -> ajaxReturn(['status' => 1,'mess' => '已禁用','data' => 0]);
			}
		}
		$this -> ajaxReturn(['status' => 0,'mess' => '非法操作！']);
	}


}